<?php

	namespace application\model;

		class JBAccount
		{

			private $myMessage = array();
			private $myTempName = "";
			private $userIsLoggedin;

			public function getMyMessage()
			{
				return $this->myMessage;
			}


			public function getmyTempName()
			{
				return $this->myTempName;
			}


			public function createMyAccount()
			{
				if (empty($_POST["LoginView::NewUserName"]) || empty($_POST["LoginView::NewPassword"]) || empty($_POST["LoginView::RepeatNewPassword"]))
				{
					$this->myMessage[] = "Alla fälten måsta vara ifyllda!";
				}

				if (isset($_POST["LoginView::NewUserName"]))
				{
					$userN = $_POST["LoginView::NewUserName"];
					$userP = $_POST["LoginView::NewPassword"];
					$userPrepeat = $_POST["LoginView::RepeatNewPassword"];
					$userN = $this->sanitizeString($userN);
					$userP = $this->sanitizeString($userP);
					$userPrepeat = $this->sanitizeString($userPrepeat);
					$userName = trim($userN);
					$userPassW = trim($userP);

					if (strlen($userName) < 3)
					{
						$this->myMessage[] = "Namnet måste ha minst 3 tecken";
						$this->myTempName = "";
					}
					if (strlen($userName) > 9)
					{
						$this->myMessage[] = "Namnet får inte he fler än 9 tecken";
						$this->myTempName = "";
					}
					else
					{
						$this->myTempName = $userName;
					}

					if (isset($_POST["LoginView::NewPassword"]) || isset($_POST["LoginView::RepeatNewPassword"]))
					{
						if (strlen($userPassW) < 6)
						{
							$this->myMessage[] = "Lösenordet måste ha minst 6 tecken";
							return;
						}

						if (isset($_POST["LoginView::NewPassword"]) && isset($_POST["LoginView::RepeatNewPassword"]))
						{

							if ($userP != $userPrepeat)
							{
								$this->myMessage[] = "Lösenorden matchar inte!";
								return;
							}
						}
					}

					else
					{
						$this->myMessage[] = "Vad hände nu?";
					}

					$newUserList = new \login\model\Userlist();

					$isUserNameTaken = $newUserList->userNameIsTaken($userName);
					if ($isUserNameTaken == false)
					{
						$this->myMessage[] = "Namnet var upptaget, försök igen";
						return;
					}
					
					$newUserList->createNewUser($userName, $userPassW);

					$this->myMessage[] = "Registreringen lyckades!";
					$this->userIsLoggedin = true;
				}
			}


			public function isUserLoggedIn()
			{
				return $this->userIsLoggedin;

			}


			public function sanitizeString($string)
			{
				$original = $string;

				$string = strip_tags($string);

				if ($original != $string)
				{
					$this->myMessage[] = "Lösenordet innehöll ogiltiga tecken som nu är borttagna!";

				}
				$string = ltrim($string);
				$string = rtrim($string);
				return $string;
			}


		}
